#include "fpnumber.h"
#include <cassert>
#include <cmath>

FPNumber::FPNumber(const FPNumber& o) : v(o.v), p(o.p) {} 
FPNumber::FPNumber(char ip) : v(0.0),p(ip) {}
FPNumber::FPNumber(double iv, char ip) : v(iv),p(ip) { trunc(); }
FPNumber& FPNumber::operator+=(FPNumber o) {
	assert(p==o.p);
	v+=o.v; trunc();
	return *this;
}
FPNumber& FPNumber::operator-=(FPNumber o) {
	assert(p==o.p);
	v-=o.v; trunc();
	return *this;
}
FPNumber& FPNumber::operator*=(FPNumber o) {
	assert(p==o.p);
	v*=o.v; trunc();
	return *this;
}
FPNumber& FPNumber::operator/=(FPNumber o) {
	assert(p==o.p);
	v/=o.v; trunc();
	return *this;
}
FPNumber& FPNumber::operator^=(FPNumber o) {
	assert(p==o.p);
	v=pow(v,o.v); trunc();
	return *this;
}
FPNumber FPNumber::operator+(FPNumber o) const { 
	FPNumber r(*this); r+=o; return r; 
}
FPNumber FPNumber::operator-(FPNumber o) const { 
	FPNumber r(*this); r-=o; return r; 
}
FPNumber FPNumber::operator*(FPNumber o) const { 
	FPNumber r(*this); r*=o; return r; 
}
FPNumber FPNumber::operator/(FPNumber o) const { 
	FPNumber r(*this); r/=o; return r; 
}
FPNumber FPNumber::operator^(FPNumber o) const { 
	FPNumber r(*this); r^=o; return r; 
}
void FPNumber::trunc() {
	(*((long long*)&v)) &= ~((1LL<<(long long)(maxP-p))-1);
}
bool FPNumber::operator==(FPNumber o) const { return v==o.v; }
bool FPNumber::operator!=(FPNumber o) const { return v!=o.v; }
bool FPNumber::operator<(FPNumber o) const { return v<o.v; }
bool FPNumber::operator<=(FPNumber o) const { return v<=o.v; }
bool FPNumber::operator>(FPNumber o) const { return v>o.v; }
bool FPNumber::operator>=(FPNumber o) const { return v>=o.v; }
char FPNumber::prec() const { return p; }
double FPNumber::asDouble() const { return v; }
ostream& operator<<(ostream& o,FPNumber f) {
	return o << f.v;
}
istream& operator>>(istream& i,FPNumber f) {
	i >> f.v;
	if (i.peek()==':') {
		int p; i.get(); i >> p;	f.p=p;
	}
	cout << f.p << " " << f.v << endl;
	f.trunc();
	return i;
}
